﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dln.Common.Helper;
using Dln.Model;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie userCookie = HttpContext.Current.Request.Cookies["LoginInfo"];
        //if (userCookie != null)
        //{
        //    if (userCookie["UserName"] != null)
        //    {
        //        Response.Redirect(String.Format("Index.aspx", userCookie["IdNum"]));
        //    }
        //}
    }

    /// <summary>
    /// 登录
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void txtLogin_Click(object sender, EventArgs e)
    {
        string strUserName = this.txtLoginName.Text.Trim();
        string strPassword = this.txtPassward.Text.Trim();
        if (string.IsNullOrEmpty(strUserName) || string.IsNullOrEmpty(strPassword))
        {
            this.spanError.InnerText = "用户名或密码不能为空！";
            return;
        }

        var employeeModel = SqlHelper.Context.From<Employee>().Where(d => d.Phone == strUserName && d.LoginPassword == strPassword).ToList();
        if (employeeModel == null || employeeModel.Count <= 0)
        {
            this.spanError.InnerText = "登录名或密码不正确！";
            return;
        }
        HttpCookie userCookie = this.Request.Cookies["LoginInfo"];
        if (userCookie != null)
        {
            this.Response.Cookies.Remove("LoginInfo");
        }
        userCookie = new HttpCookie("LoginInfo");
        userCookie.Values["UserName"] = strUserName;
        userCookie.Values["Password"] = strPassword;
        userCookie.Expires = DateTime.Now.AddDays(1);
        this.Response.AppendCookie(userCookie);
        this.Response.Redirect("Index.aspx");
    }


    #region IP
    //获取IP
    public string GetClientIP()
    {
        string result = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (null == result || result == String.Empty)
        {
            result = Request.ServerVariables["REMOTE_ADDR"];
        }
        if (null == result || result == String.Empty)
        {
            result = Request.UserHostAddress;
        }
        return result;
    }
    #endregion

    /// <summary>
    /// 注册
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void txtRegister_Click(object sender, EventArgs e)
    {
        this.Response.Redirect("Register.aspx");
    }
}